package com.skivingcloud.admin.sys.controller;

import com.mybatisflex.core.paginate.Page;
import com.skivingcloud.admin.sys.entity.SysLog;
import com.skivingcloud.admin.sys.service.SysLogService;
import com.skivingcloud.common.utils.PageParameter;
import com.skivingcloud.common.utils.ReturnT;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 *  控制层。
 *
 * @author skivingcloud
 * @since 2023-08-28
 */
@Tag(name = "SysLogController", description = "系统日志API")
@RestController
@RequestMapping("/sysLog")
public class SysLogController {
    
    private static final Logger log = LoggerFactory.getLogger(SysLogController.class);
    
    @Resource
    private SysLogService sysLogService;
    
    /**
     * 分页查询日志
     * @param parameter 查询参数
     * @return 分页查询结果
     */
    @Operation(summary = "分页查询日志", description = "日志列表",
            parameters = {@Parameter(name = "parameter", description = "分页查询条件")})
    @ApiResponse(responseCode = "2xx",description = "分页日志列表")
    @PostMapping("/queryByPage")
    public ReturnT<Page<SysLog>> queryByPage(@RequestBody PageParameter<Map<String, Object>> parameter){
        Page<SysLog> page = sysLogService.queryByPage(parameter);
        return ReturnT.ok(page);
    }
    
    /**
     * 添加。
     *
     * @param sysLog 日志对象
     * @return {@code true} 添加成功，{@code false} 添加失败
     */
    @PostMapping("/save")
    @Operation(summary="新增日志")
    public boolean save(@RequestBody SysLog sysLog){
        try{
            return sysLogService.save(sysLog);
        }catch(Exception e){
            log.error(e.getMessage(), e);
            return false;
        }
    }
    
    /**
     * 根据主键获取详细信息。
     *
     * @param id 主键
     * @return 详情
     */
    @GetMapping("/getInfo/{id}")
    @Operation(summary="根据id获取日志信息")
    public SysLog getInfo(@PathVariable(value = "id") String id){
        return sysLogService.getById(id);
    }
}
